/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.CarBean;
import DAL.CarBookBean;
import java.io.*;
import java.util.StringTokenizer;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerCarBook", urlPatterns = {"/ControllerCarBook"})
public class ControllerCarBook extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        CarBean carb=new CarBean();
        CarBookBean carBB=new CarBookBean();
        if(request.getParameter("booking")!=null)
        {
            if(request.getParameter("booking").equals("Rental"))
            {
                String username=request.getParameter("username");
                String carid=request.getParameter("carid");
                String driver=request.getParameter("driver");
                String PickUpDate=request.getParameter("PickUpDate");
                StringTokenizer  str=new StringTokenizer(PickUpDate,"-");
                int day=Integer.parseInt(str.nextToken());
                int month=Integer.parseInt(str.nextToken());
                String year=str.nextToken();
                
                String DropOffDate=request.getParameter("DropOffDate");
                StringTokenizer  str1=new StringTokenizer(DropOffDate,"-");
                
                int day1=Integer.parseInt(str1.nextToken());
                int month1=Integer.parseInt(str1.nextToken());
                String year1=str1.nextToken();
                
                int number=Integer.parseInt(request.getParameter("songay"));
                float pricepackage=0;
                String pack= request.getParameter("package");
                
               //out.println(day+":"+month+":"+day1+":"+month1);
                if(day > day1 && month >= month1)
                {
                     out.println("<script>alert('Sorry! Please choose pick up date less than drop off date');javascript:window.history.back(-1);</script>");
                }
                else if(day < day1 && month >month1)
                {
                    out.println("<script>alert('Sorry! Please choose pick up date less than drop off date');javascript:window.history.back(-1);</script>");
                }
                else
                {
                    if(request.getParameter("package")!=null)
                    {            
                        if (request.getParameter("package").equals("Day"))
                        {
                            pricepackage=carb.seletePriceDay(carid);
                        }
                        if(request.getParameter("package").equals("Hour"))
                        {
                            pricepackage=carb.seletePriceHour(carid);
                        }
                         if(request.getParameter("package").equals("kilomet"))
                        {
                            pricepackage=carb.seletePricekilomet(carid);
                        }
                    }

                    HttpSession session=request.getSession(true);
                    session.setAttribute("carid",carid);
                    session.setAttribute("driver",driver);
                    session.setAttribute("PickUpDate",PickUpDate);
                    session.setAttribute("DropOffDate",DropOffDate);
                    session.setAttribute("number",number);
                    session.setAttribute("pack",pack);
                    session.setAttribute("totalmoney",pricepackage * number);
                    //out.println("sjdhsdk");
                   response.sendRedirect("index.jsp?page=DetailBook");
                }
            }
        }
        if(request.getParameter("comfirm")!=null)
        {
            if(request.getParameter("comfirm").equals("Comfirm"))
            {
                String username=request.getParameter("username");
                String carid=request.getParameter("carid");
                String driver=request.getParameter("driver");
                String PickUpDate=request.getParameter("PickUpDate");
                
                StringTokenizer  str=new StringTokenizer(PickUpDate,"-");
                int day=Integer.parseInt(str.nextToken());
                int month=Integer.parseInt(str.nextToken());
                String year=str.nextToken();
                String pic= month+"/"+day+"/"+year; 
                
                String DropOffDate=request.getParameter("DropOffDate");
                StringTokenizer  str1=new StringTokenizer(DropOffDate,"-");
                int day1=Integer.parseInt(str1.nextToken());
                int month1=Integer.parseInt(str1.nextToken());
                String year1=str1.nextToken();
                String drop= month1+"/"+day1+"/"+year1;     
                              
                  //  SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
                    int number=Integer.parseInt(request.getParameter("number"));
                    String pack= request.getParameter("pack");
                    String timer=number+" "+pack;
                    float totalmoney=Float.parseFloat(request.getParameter("totalmoney"));
                    //out.println("ok");
                    boolean rs=carBB.InserCarBook(username,carid,driver,pic,drop,timer,totalmoney);
                    HttpSession session=request.getSession(true);
                    session.removeAttribute("carid");
                    session.removeAttribute("driver");
                    session.removeAttribute("PickUpDate");
                    session.removeAttribute("DropOffDate");
                    session.removeAttribute("number");
                    session.removeAttribute("pack");
                    session.removeAttribute("totalmoney");
                    if(rs==true)
                    {
                        out.println("<script>alert('Car Booking Success!');window.location='index.jsp?page=MyCarBook';</script>");
                    }
                    else
                    {
                        out.println("<script>alert('Sorry for the inconvenience!. Please input detail car booking!');javascript:window.history.back(-1);</script>");
                    }
                }
        }
        if(request.getParameter("cancel")!=null)
        {
            if(request.getParameter("cancel").equals("Cancel"))
            {
             HttpSession session=request.getSession(true);
                session.removeAttribute("carid");
                session.removeAttribute("driver");
                session.removeAttribute("PickUpDate");
                session.removeAttribute("DropOffDate");
                session.removeAttribute("number");
                session.removeAttribute("pack");
                session.removeAttribute("totalmoney");
                response.sendRedirect("index.jsp?page=ListCars");
            }
        }
//        if(request.getParameter("delete")!=null)
//        {
//            if(request.getParameter("delete").equals("Delete"))
//            {
//                int id=Integer.parseInt(request.getParameter("bookingID"));
//                boolean rs=carBB.DeleteCarBook(id);
//                if(rs==true)
//                {
//                    out.println("<script>alert('Delete cars rental success!');window.location='Admin/management.jsp?page=ListRentalCar';</script>");
//                }
//                else
//                {
//                     out.println("<script>alert('Delete car rental fail back!');javascript:window.history.back(-1);</script>");
//                    //System.out.println("<a href='Admin/management.jsp?page=CarBooking'>Delete fail back...!</a>");
//                }
//            }
//        }
        if(request.getParameter("delete1")!=null)
        {
            if(request.getParameter("delete1").equals("Delete"))
            {
                int id=Integer.parseInt(request.getParameter("bookingID"));
                boolean rs=carBB.DeleteCarBook(id);
                if(rs==true)
                {
                    out.println("<script>alert('Delete car cancel success!');window.location='Admin/management.jsp?page=ListRentalCar';</script>");
                }
                else
                {
                    out.println("<script>alert('Delete car cancel fail back!');javascript:window.history.back(-1);</script>");
                    //System.out.println("<a href='Admin/management.jsp?page=CarCancel'>Delete fail back...!</a>");
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
